<?php
/*
    Id:     $Id: $
    File:   Error.php
    Author: John Griffin
    Date:   May 26, 2009

    Description:
*/
class Error implements Iterator, Countable {
    private $error = null;          // array containing the errors
    private $position = 0;          // current position for the iterator
    const NOT_IMPLEMENTED = 0;
    const REQUIRED_FIELD_MISSING = 1;
    const BAD_VALUE = 2;
    const BAD_TYPE = 3;
    const BAD_INTEGER_OR_EXPRESSION = 4;
    const BAD_REAL_OR_EXPRESSION = 5;
    const BAD_IDS = 6;
    
    public function __construct () {
        $this->error = array ();
    }
    
    public function addError ($code, $field, $message) {
        $this->error[] = array ('code' => $code, 'field' => $field, 'message' => $message);
    }
    
    public function hasErrors () {
        return (count ($this->error) > 0);
    }
    
    /* these next few methods are to implement an iterator */    
    public function rewind () {
        $this->position = 0;
    }
    
    public function current () {
        return ($this->valid ()) ? $this->error[$this->position] : null;
    }
    
    public function key () {
        return $this->position;
    }
    
    public function next () {
        return ($this->position < count ($this->error)) ? $this->position++ : false;
    }
    
    public function valid () {
        return array_key_exists ($this->position, $this->error);
    }
    /* end of iterator functions */
    
    /* this next method is for the countable interface */
    public function count () {
        return count ($this->error);
    }
}
?>
